package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.EditInfo;
import com.tiza.xiaoyoubao.entity.IdCardInfo;
import com.tiza.xiaoyoubao.entity.TaoBaoAccountInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.entity.bean.BankInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import org.simple.eventbus.Subscriber;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/3/5.
 * 账号绑定
 */

public class AccountBindingFragment extends BaseFragment {

    @BindView(R.id.view_idCard)
    View view_idCard;
    @BindView(R.id.view_bank_card)
    View view_bank_card;
    @BindView(R.id.view_qq)
    View view_qq;
    @BindView(R.id.view_taobao)
    View view_taobao;
    @BindView(R.id.view_beautiful_say)
    View view_beautiful_say;
    @BindView(R.id.view_jingdong)
    View view_jingdong;
    @BindView(R.id.view_mogujie)
    View view_mogujie;

    @BindView(R.id.tv_idCard_info)
    TextView tv_idCard_info;
    @BindView(R.id.tv_bankCard_info)
    TextView tv_bankCard_info;
    @BindView(R.id.tv_qq_info)
    TextView tv_qq_info;
    @BindView(R.id.tv_taobao_info)
    TextView tv_taobao_info;
    @BindView(R.id.tv_beautiful_say_info)
    TextView tv_beautiful_say_info;
    @BindView(R.id.tv_jingdong_info)
    TextView tv_jingdong_info;
    @BindView(R.id.tv_mogujie_info)
    TextView tv_mogujie_info;
    IdCardInfo idCardInfo;
    BankInfo bankInfo;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_account_binding;
    }


    /**
     * @param info
     */
    @Subscriber(tag = Constants.TAG_SET_USERINFO_SUCCESS)
    private void onEdit(EditInfo info) {
        int type = info.getType();
        switch (type) {
            case EditInfo.TYPE_GET_QQ_NAME:
                String QQ = info.getValue();
                tv_qq_info.setText(QQ);
                break;
        }
    }

    @Subscriber(tag = Constants.TAG_SET_BANKINFO_SUCCESS)
    private void setBankInfoSuccess(boolean prmB) {
        tv_bankCard_info.setText("修改");
        tv_bankCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
        getBankInfo();
    }

    @Subscriber(tag = Constants.TAG_SET_BUYERIDCARD_SUCCESS)
    private void setBuyerIdCardSuccess(boolean prmB) {
        tv_idCard_info.setText("已绑定");
        tv_idCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
        getBuyerIdCard();
    }
    @Subscriber(tag = Constants.TAG_SET_BUYERTAOBAO_SUCCESS)
    private void setBuyerTaoBaoSuccess(boolean prmB) {
        getBankTaoBao();
    }
    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        getBuyerIdCard();
        getBankInfo();
        getBankTaoBao();
        if (!TextUtils.isEmpty(userInfo.getWebzzCmsBuyerInfo().getQqCard())) {
            tv_qq_info.setText(userInfo.getWebzzCmsBuyerInfo().getQqCard());
        } else {
            tv_qq_info.setText("未绑定");
        }
    }

    @OnClick({
            R.id.view_idCard,
            R.id.view_bank_card,
            R.id.view_qq,
            R.id.view_taobao,
            R.id.view_beautiful_say,
            R.id.view_jingdong,
            R.id.view_mogujie
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_idCard:
                Bundle args=new Bundle();
                args.putSerializable("info",idCardInfo);
                UIHelper.showIDCertification(mActivity,args);
                break;
            case R.id.view_bank_card:
                Bundle args1=new Bundle();
                args1.putSerializable("info",bankInfo);
                UIHelper.showBankInfo(mActivity,args1);
                break;
            case R.id.view_qq:
                UIHelper.showQQBingding(mActivity);
                break;
            case R.id.view_taobao:
                UIHelper.showTaoBaoBingding2(mActivity);
                break;
            case R.id.view_beautiful_say:
                UIHelper.showMeiLiSayBingding(mActivity);
                break;
            case R.id.view_jingdong:
                UIHelper.showJingDongBingding(mActivity);
                break;
            case R.id.view_mogujie:
                UIHelper.showMoGuJieBingding(mActivity);
                break;
            default:
                break;
        }

    }

    private void getBuyerIdCard() {
        ApiHttpClient.getBuyerIdCard(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                                     new JsonCallback<IdCardInfo>(IdCardInfo.class) {
                                         @Override
                                         public void onError(Call call, Exception e) {
                                             toast("网络异常，请检查后再试");
                                         }

                                         @Override
                                         public void onResponse(ResponseInfo<IdCardInfo> response) {
                                             int code = response.getCode();
                                             if (code == 1) {
                                                 showWarnDialog(response.getMessage());
                                                 return;
                                             }
                                             List<IdCardInfo> data = response.getData();
                                             if (null == data.get(0) || data.size() == 0) {
                                                 return;
                                             }
                                             idCardInfo = data.get(0);
                                             if(idCardInfo.getAuth().equals("0")){
                                                 tv_idCard_info.setText("审核中");
                                                 tv_idCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
                                             }else if(idCardInfo.getAuth().equals("1")){
                                                 tv_idCard_info.setText("已绑定");
                                                 tv_idCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
                                             }else if(idCardInfo.getAuth().equals("2")){
                                                 tv_idCard_info.setText("未通过");
                                                 tv_idCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
                                             }
                                         }
                                     }
        );
    }

    private void getBankInfo() {
        ApiHttpClient.getBuyerBankInfo(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                                     new JsonCallback<BankInfo>(BankInfo.class) {
                                         @Override
                                         public void onBefore(Request request) {
                                             super.onBefore(request);
                                         }

                                         @Override
                                         public void onError(Call call, Exception e) {
                                             toast("网络异常,请稍后重试");
                                         }

                                         @Override
                                         public void onResponse(ResponseInfo<BankInfo> response) {
                                             int code = response.getCode();
                                             String message = response.getMessage();
                                             if (code == 1) {
                                                 showWarnDialog(message);
                                                 return;
                                             }
                                             List<BankInfo> data = response.getData();
                                             if (null == data || data.size() == 0) {
                                                 return;
                                             }
                                             bankInfo = data.get(0);
                                             if (bankInfo.getAuth().equals("0")) {
                                                 tv_bankCard_info.setText("审核中");
                                                 tv_bankCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
                                             } else if (bankInfo.getAuth().equals("1")) {
                                                 tv_bankCard_info.setText("已绑定");
                                                 tv_bankCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
                                             } else if (bankInfo.getAuth().equals("2")) {
                                                 tv_bankCard_info.setText("未通过");
                                                 tv_bankCard_info.setTextColor(getResources().getColor(R.color.textColorContact));
                                             }
                                         }

                                         @Override
                                         public void onAfter() {
                                             super.onAfter();
                                         }
                                     }
        );
    }

    private void getBankTaoBao() {
        ApiHttpClient.getBuyerTaoBao(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                                     new JsonCallback<TaoBaoAccountInfo>(TaoBaoAccountInfo.class) {
                                         @Override
                                         public void onBefore(Request request) {
                                             super.onBefore(request);
                                         }

                                         @Override
                                         public void onError(Call call, Exception e) {
                                             toast("网络异常,请稍后重试");
                                         }

                                         @Override
                                         public void onResponse(
                                                 ResponseInfo<TaoBaoAccountInfo> response
                                         ) {
                                             int code = response.getCode();
                                             String message = response.getMessage();
                                             if (code == 1) {
                                                 showWarnDialog(message);
                                                 return;
                                             }
                                             List <TaoBaoAccountInfo>mList = response.getData();
                                             if (null == mList || mList.size() == 0) {
                                                 tv_taobao_info.setVisibility(View.VISIBLE);
                                                 return;
                                             }
                                             if (mList.size() >0) {
                                                 tv_taobao_info.setVisibility(View.GONE);
                                             }
                                         }

                                         @Override
                                         public void onAfter() {
                                             super.onAfter();
                                         }
                                     }
        );
    }

}
